//
//  Toast.swift
//  
//
//  Created by tank on 2021/12/5.
//

import Foundation
import SwiftMessages

public class Toast {

    public typealias ToastCompletion = () -> Void

    public static let shared: Toast = Toast()

    private init() {

    }

    public func loading(message: String, interactive: Bool?, completion: ToastCompletion?) {
        toast(type: .loading, message: message, interactive: interactive, completion: completion)
    }

    private func toast(type: ToastView.ToastType?, message: String, interactive: Bool?, completion: ToastCompletion?) {

        let toastView = ToastView(type: type, message: message)

        var config = SwiftMessages.defaultConfig
        config.presentationStyle = .center
        config.dimMode = .gray(interactive: interactive ?? false)
        config.duration = .forever
        config.interactiveHide = false
        config.eventListeners.append { event in
            if event == .didHide {
                completion?()
            }
        }
        SwiftMessages.show(config: config, view: toastView)
    }

    public func closeToast() {
        SwiftMessages.hideAll()
    }
}
